package com.esun.service.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.fastjson.JSONObject;
import com.esun.api.request.ExampleRequestDto;
import com.esun.api.request.MongoSyncRequestDto;
import com.esun.api.response.ExampleResponseDto;
import com.esun.common.constant.MongoConstant;
import com.esun.common.constant.SystemsConstant;
import com.esun.common.enums.Message;
import com.esun.common.utils.FileUtils;
import com.esun.common.utils.GuidUtils;
import com.esun.common.utils.MessageUtils;
import com.esun.dao.das.ExampleDas;
import com.esun.dao.eo.ExampleEo;
import com.esun.dao.eo.PageEo;
import com.esun.dao.eo.SortEo;
import com.esun.service.service.ExampleService;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 样例表(Example)表服务实现类
 *
 * @author John.xiao
 * @since 2021-08-17 15:36:40
 */
@Service
@Slf4j
public class ExampleServiceImpl implements ExampleService {
    @Resource
    private ExampleDas exampleDas;

    @Value("${file.path:/}")
    String filePath;



    @Override
    public PageInfo<ExampleResponseDto> getExampleByPage(ExampleRequestDto requestDto) {
        List<ExampleResponseDto> responseDtoList = new ArrayList<>();
        PageEo pageEo = PageEo.builder().pageIndex(requestDto.getPageIndex()).pageSize(requestDto.getPageSize()).build();
        ExampleEo exampleEo = ExampleEo.builder().build();
        BeanUtils.copyProperties(requestDto,exampleEo);
        List<SortEo> sortList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(requestDto.getSortList())){
            requestDto.getSortList().forEach((sortRequestDto -> {
                sortList.add(SortEo.builder().sortParam(sortRequestDto.getSortParam()).ascending(sortRequestDto.getAscending()).build());
            }));
        }
        PageInfo<ExampleEo> pageInfo = exampleDas.queryPageBySort(exampleEo,pageEo, sortList);
        List<ExampleEo> eoList = pageInfo.getList();
        for (ExampleEo eo : eoList){
            ExampleResponseDto responseDto = new ExampleResponseDto();
            BeanUtils.copyProperties(eo,responseDto);
            responseDtoList.add(responseDto);
        }
        PageInfo<ExampleResponseDto> responseDtoPageInfo = new PageInfo<>(responseDtoList);
        responseDtoPageInfo.setTotal(pageInfo.getTotal());
        return responseDtoPageInfo;
    }

    @Override
    public List<ExampleResponseDto> addExample(List<ExampleRequestDto> requestDtoList) {
        List<ExampleEo> eoList = new ArrayList<>();
        List<ExampleResponseDto> responseDtoList = new ArrayList<>();
        for (ExampleRequestDto requestDto : requestDtoList){
            ExampleEo exampleEo = ExampleEo.builder().build();
            BeanUtils.copyProperties(requestDto,exampleEo);
            ExampleResponseDto responseDto = new ExampleResponseDto();
            BeanUtils.copyProperties(exampleEo,responseDto);
            String code = Message.QUERY_SUCCESS.getCode();
            responseDto.setErrorCode(code);
            responseDto.setMsg(exampleEo.getName()+MessageUtils.getInfoByCode(code));
            responseDtoList.add(responseDto);
            eoList.add(exampleEo);
        }
        syncExampleToMongo(eoList);
        for (ExampleResponseDto responseDto : responseDtoList) {
            if (!Message.QUERY_SUCCESS.getCode().equals(responseDto.getErrorCode())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return responseDtoList;
            }
        }
        if (!CollectionUtils.isEmpty(eoList)){
            exampleDas.insertBatch(eoList);
        }
        return responseDtoList;
    }

    @Override
    public List<ExampleResponseDto> updateExample(List<ExampleRequestDto> requestDtoList) {
        List<ExampleEo> eoList = new ArrayList<>();
        List<ExampleResponseDto> responseDtoList = new ArrayList<>();
        for (ExampleRequestDto requestDto : requestDtoList){
            ExampleEo exampleEo = ExampleEo.builder().build();
            BeanUtils.copyProperties(requestDto,exampleEo);
            ExampleResponseDto responseDto = new ExampleResponseDto();
            BeanUtils.copyProperties(exampleEo,responseDto);
            String code = Message.QUERY_SUCCESS.getCode();
            responseDto.setErrorCode(code);
            responseDto.setMsg(exampleEo.getName() + MessageUtils.getInfoByCode(code));
            responseDtoList.add(responseDto);
            eoList.add(exampleEo);
        }
        for (ExampleResponseDto responseDto : responseDtoList) {
            if (!Message.QUERY_SUCCESS.getCode().equals(responseDto.getErrorCode())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return responseDtoList;
            }
        }
        for (ExampleEo eo: eoList) {
            exampleDas.update(eo);
        }
        return responseDtoList;
    }

    @Override
    public List<ExampleResponseDto> deleteExample(List<ExampleRequestDto> requestDtoList) {
        List<ExampleEo> eoList = new ArrayList<>();
        List<ExampleResponseDto> responseDtoList = new ArrayList<>();
        for (ExampleRequestDto requestDto : requestDtoList){
            ExampleEo exampleEo = ExampleEo.builder().build();
            BeanUtils.copyProperties(requestDto,exampleEo);
            ExampleResponseDto responseDto = new ExampleResponseDto();
            BeanUtils.copyProperties(exampleEo,responseDto);
            String code = Message.QUERY_SUCCESS.getCode();
            responseDto.setErrorCode(code);
            responseDto.setMsg(exampleEo.getName() + MessageUtils.getInfoByCode(code));
            responseDtoList.add(responseDto);
            eoList.add(exampleEo);
        }
        for (ExampleResponseDto responseDto : responseDtoList) {
            if (!Message.QUERY_SUCCESS.getCode().equals(responseDto.getErrorCode())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return responseDtoList;
            }
        }
        if(!CollectionUtils.isEmpty(eoList)){
            exampleDas.deleteByIdBatch(eoList);
        }
        return responseDtoList;
    }

    @Override
    public List<ExampleResponseDto> importExample(List<ExampleEo> eoList) {
        List<ExampleResponseDto> exampleResponseDtoList = Lists.newArrayList();
        for (ExampleEo eo : eoList){
            ExampleResponseDto responseDto = new ExampleResponseDto();
            BeanUtils.copyProperties(eo,responseDto);
            exampleResponseDtoList.add(responseDto);
        }
        return exampleResponseDtoList;
    }

    @Override
    public void exportExample(ExampleRequestDto requestDto) {
        ExampleEo exampleEo = ExampleEo.builder().build();
        BeanUtils.copyProperties(requestDto,exampleEo);
        List<ExampleEo> eoList = exampleDas.queryAll(exampleEo);
        String programUrl = System.getProperty("user.dir");
        String fileName = programUrl.concat(filePath).concat(GuidUtils.getGuid()).concat(SystemsConstant.HEADQUARTERS);
        EasyExcel.write(fileName, ExampleEo.class).sheet("用户信息导出文件").doWrite(eoList);
        FileUtils fileUtils = new FileUtils();
        fileUtils.downLoad(fileName);
    }

    @Override
    public void downloadTemplate(String path) {
        String programUrl = System.getProperty("user.dir");
        path = programUrl + path;
        FileUtils fileUtils = new FileUtils();
        fileUtils.downLoad(path);
    }


    private void syncExampleToMongo(List<ExampleEo> eoList) {
        MongoSyncRequestDto syncRequestDto = new MongoSyncRequestDto();
        syncRequestDto.setCollection("example");
        syncRequestDto.setRequestMethod(MongoConstant.INSERT);
        syncRequestDto.setEntityJson(JSONObject.toJSONString(eoList));
    }
}